package com.situ.car.controller;

import com.situ.car.pojo.entity.RReturn;
import com.situ.car.pojo.query.ReturnQuery;
import com.situ.car.pojo.vo.OrderVo;
import com.situ.car.pojo.vo.ReturnVo;
import com.situ.car.service.ICarService;
import com.situ.car.service.IOrderService;
import com.situ.car.service.IReturnService;
import com.situ.car.service.Impl.CarServiceImpl;
import com.situ.car.service.Impl.OrderServiceImpl;
import com.situ.car.service.Impl.ReturnServiceImpl;
import com.situ.car.util.MyBeanUtils;
import com.situ.car.util.PageInfo;
import com.situ.car.util.Result;
import org.springframework.util.ObjectUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/return")
public class ReturnController extends BaseController{
    private IReturnService returnService = new ReturnServiceImpl();
    private IOrderService orderService = new OrderServiceImpl();
    private ICarService carService = new CarServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ReturnController.service");
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "deleteById" :
                deleteById(req, resp);
                break;
            case "deleteAll" :
                deleteAll(req, resp);
                break;
            case "update" :
                update(req, resp);
                break;
            case "selectById" :
                selectById(req, resp);
                break;
        }
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("ReturnController.selectById");
        String id = req.getParameter("id");
        ReturnVo returnVo = returnService.selectById(Integer.parseInt(id));
        toJSON(resp, Result.ok(returnVo));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("ReturnController.update");
        RReturn rReturn = MyBeanUtils.copyToBean(req, RReturn.class);
        returnService.update(rReturn);
        toJSON(resp, Result.ok("修改成功！"));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("ReturnController.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        if(!ObjectUtils.isEmpty(ids)) {
            returnService.deleteAll(ids);
            toJSON(resp, Result.ok("删除成功！"));
        } else {
            toJSON(resp, Result.error("没有数据要删除！"));
        }
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("ReturnController.deleteById");
        String id = req.getParameter("id");
        returnService.deleteById(Integer.parseInt(id));
        toJSON(resp, Result.ok("删除成功！"));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("ReturnController.add");
        RReturn rReturn = MyBeanUtils.copyToBean(req, RReturn.class);
        returnService.add(rReturn);
        String rentalId = req.getParameter("rentalId");
        orderService.updateOrdersStatus(Integer.parseInt(rentalId));
        carService.updateCarStatus(Integer.parseInt(rentalId));
        toJSON(resp, Result.ok("还车成功！"));
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("ReturnController.selectByPage");
        ReturnQuery returnQuery = MyBeanUtils.copyToBean(req, ReturnQuery.class);
        PageInfo pageInfo = returnService.selectByPage(returnQuery);
        toJSON(resp, pageInfo);
    }
}
